/**
 * Created by 145185 on 2017/5/27.
 */
import BigImg from '../../../components/BigImg-pc/index.vue'
import Util from 'assets/js/util'
export default {
  data () {
    return {
      //照片放大
      imgIndex: -1,
      imgSwitch: false,
      photoList: [],
      isRectangle: false,
      pageType: 'handle',       //handle 处理举报 ；view 详情
      handlingReport: {},
      report: { //处理评论
        reportType: '1',
        reportChannel: '',
        reportResult: '',
        reportStatus: 1,
        title: ""
      },
      recordModal: false, //modal开关
      handleRecordList: false,//处理历史记录//处理列表
      handleRecordCount: 0,
      pageSize: 10,
      pageNum: 1,
      dataTotal: 1,
      pageCount: 1,
      reportId: -1,
      isConfirm: false,  //是否办结结束
      viewNotify: false,//通知预览modal开关
      notifyText: '',    //通知文本
      zfhfReplyList: [],
      isSubmitZfhf: false,
      noticekey:'noticekey1', //Notice弹窗name
      showWarnModal:false, //格式校验提示框
      warnContent:'',  //提示文案
      submitSwitch:true
    }
  },
  mounted () {
    this.init();
  },

  watch: {
    // 如果路由有变化，会再次执行该方法
    'handleRecordList': function () {
      this.handleRecordCount = this.dataTotal;
    }
  },
  computed: {
    btnText: function () {//提交按钮文本
      if (this.isConfirm) {
        this.report.reportStatus = 2;
        return '办结'
      } else {
        this.report.reportStatus = 1;
        return '保存并继续处理<i class="icon icon-arrow-right"></i>'
      }
    }
  },
  methods: {
    init: function (newRoute, oldRoute) {
      let routeName = this.$route.name
      if (routeName == "reportHandleDetail") {
        this.reportId = this.$route.query.reportId;
        this.pageType = this.$route.params.pageType;
        Util.setTitle('处理举报');
        this.queryReportById()
        if (this.pageType == 'view') {
          let param = {
            id: this.reportId,
            pageNum: this.pageNum,
            pageSize: this.pageSize
          }
          this.queryRecordList(param)
        }
      }
    },
    goToSubject: function(item){
      this.$router.push({
        name: "subjectDetail",
        query:{
          subjectId:item.subjectId
        }
      })
    },
    /**
     * 获取举报详情
     */
    queryReportById(){
      let _this = this;
      let param = {
        id: this.reportId,
        isEdit:this.pageType=='handle'?1:0
      };
      this.$http.post('convphotoManage/queryReportById',param)
        .then(function (data) {
          if (data.meta.code == '0') {
            document.body.scrollTop = 0;
            _this.handlingReport = data.data;
            _this.photoList = data.data.photos;
            _this.zfhfReplyList = data.data.zfhfInfo;
            _this.handleRecordCount = data.data.handleRecordCount || 0;
            if(_this.handlingReport.showFlag&&_this.handlingReport.showFlag==1){
              _this.showFlag= true;
            }else{
              _this.showFlag=  false;
            }
          } else {
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: data.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = data.meta.message;
          }
        })
        .catch(function (err) {
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: err.meta.message
          // });
          _this.showWarnModal = true;
          _this.warnContent = err.meta.message;
        })
    },

    /**
     * 显示处理历史modal
     */
    viewRecord(){
      this.search(1);
      this.recordModal = true;
    },
    /**
     * 返回上一页
     */
    goBackList(){
      let param = {
        convId: this.reportId
      }, _this = this;
      this.$http.post('convphotoManage/unlockAuditRecord', param)
        .then(function (data) {
          if (data.meta.code == '0') {
            _this.$router.replace({name: 'reportHandlingList'})
          } else {
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: data.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = data.meta.message;
          }
        })
        .catch(function (err) {
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: err.meta.message
          // });
          _this.showWarnModal = true;
          _this.warnContent = err.meta.message;
        })
    },
    viewNotifyInfo(){
      let flag = this.comCheck();
      if (flag) {
        let param = {
          "id": this.reportId,
          "reportChannel": this.report.reportChannel,
          "reportResult": this.report.reportResult,
          "reportStatus": this.isConfirm ? 2 : 1,
          "reportType": this.isSubmitZfhf?2:1,
          "title": this.report.title,
          "showFlag": this.showFlag?1:2
        }, _this = this;

        //获取通知详情
        this.$http.post('convphotoManage/queryMsgContentBeforeAuditReport', param)
          .then(function (data) {
            if (data.meta.code == '0') {
              _this.viewNotify = true;
              _this.notifyText = data.data || '';
              _this.$nextTick(function(){
                document.querySelector('.view-notify-content').scrollTop = 0
              })
            } else {
              // _this.$Modal.warning({
              //   title: '温馨提示',
              //   content: data.meta.message
              // });
              _this.showWarnModal = true;
              _this.warnContent = data.meta.message;
            }
          })
          .catch(function (err) {
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: err.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = err.meta.message;
          })
      }
    },
    /**
     * 确定处理
     */
    confirm(){
      if(this.submitSwitch){
        let _this = this;
        let flag = this.comCheck();
        if (flag) {
          this.submitSwitch = false;
          this.viewNotify = false;
          let param = {
            "id": this.reportId,
            "reportChannel": this.report.reportChannel,
            "reportResult": this.report.reportResult,
            "reportStatus": this.isConfirm ? 2 : 1,
            "reportType": this.isSubmitZfhf?2:1,
            "title": this.report.title,
            "showFlag": this.showFlag?1:2,
            "msgContent":this.notifyText
          }, _this = this;
          //提交处理
          this.$http.post('convphotoManage/addReport', param)
            .then(function (data) {
              _this.submitSwitch = true;
              if (data.meta.code == '0') {
                if (_this.report.reportStatus == 1) {
                  _this.report.reportChannel = ""
                  _this.report.reportResult = ""
                  _this.handleRecordCount++
                  _this.$Notice.success({
                    title: '温馨提示',
                    desc: '操作成功',
                    duration:2,
                  });
                  //若第一次提交到首都之窗
                  if(param.reportType==2){
                    _this.isSubmitZfhf=false;
                    _this.queryReportById()
                  }
                } else {
                  _this.$router.push({name: 'reportHandlingList'});
                }
              } else {
                // _this.$Modal.warning({
                //   title: '温馨提示',
                //   content: data.meta.message
                // });
                _this.showWarnModal = true;
                _this.warnContent = data.meta.message;
              }
            })
            .catch(function (err) {
              // _this.$Modal.warning({
              //   title: '温馨提示',
              //   content: err.meta.message
              // });
              if(err.message.indexOf('timeout')>=0){
                _this.warnContent = "请求超时";
              }else{
                _this.warnContent = err.meta.message;
              }
              _this.showWarnModal = true;
              _this.submitSwitch = true;
            })
        }
        else{
          _this.submitSwitch = true;
        }
        setTimeout(function () {
          _this.submitSwitch = true;
        },30000)
      }


    },
    /**
     * 查询
     * @param pageNum
     */
    search(pageNum){
      this.pageNum = pageNum
      var param = {
        id: this.reportId,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      this.queryRecordList(param);
    },
    /**
     * 查询历史记录
     * @param param
     */
    queryRecordList(param){
      let _this = this;
      this.$http.post('convphotoManage/queryHandleHistoryById',param)
        .then(function (data) {
          if (data.meta.code == '0') {
            data = data.data;
            _this.dataTotal = data.dataTotal || 0;
            _this.pageCount = data.pageCount || 0;
            _this.handleRecordList = data.result || [];
          } else {
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: data.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = data.meta.message;
          }
        })
        .catch(function (err) {
          // _this.$Modal.warning({
          //   title: '温馨提示',
          //   content: err.meta.message
          // });
          _this.showWarnModal = true;
          _this.warnContent = err.meta.message;
        })
    },
    /**
     * 表单校验
     */
    comCheck(){
      if(this.viewNotify&&!this.notifyText.trim()){
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '消息内容不能为空'
        // });
        this.showWarnModal = true;
        this.warnContent =  '消息内容不能为空';
        return false;
      }
      if(this.viewNotify&&Util.isEmojiCharacter(this.viewNotify)){
        // this.$Modal.warning({
        //   title:'温馨提示',
        //   content:"消息内容不能包含emoji表情"
        // });
        this.showWarnModal = true;
        this.warnContent =  "消息内容不能包含emoji表情";
        return false;
      }
      if (this.isSubmitZfhf && !this.report.title.trim()) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '请填写标题'
        // });
        this.showWarnModal = true;
        this.warnContent =  '请填写标题';
        return false;
      }
      if (this.isSubmitZfhf&&this.report.title.trim().length < 2) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '标题至少2个字'
        // });
        this.showWarnModal = true;
        this.warnContent =  '标题至少2个字';
        return false;
      }
      if (this.isSubmitZfhf&&this.report.title.trim().length > 30) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '标题长度不超过100字'
        // });
        this.showWarnModal = true;
        this.warnContent =  '标题长度不超过100字';
        return false;
      }
      if(this.isSubmitZfhf&&Util.isEmojiCharacter(this.report.title)){
        // this.$Modal.warning({
        //   title:'温馨提示',
        //   content:"标题不能包含emoji表情",
        // });
        this.showWarnModal = true;
        this.warnContent =  "标题不能包含emoji表情";
        return false;
      }
      if (!this.isSubmitZfhf&&!this.report.reportChannel.trim()) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '请填写处理途径'
        // });
        this.showWarnModal = true;
        this.warnContent =  '请填写处理途径';
        return false;
      }
      if (!this.isSubmitZfhf&&this.report.reportChannel.trim().length < 2) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '处理途径至少2个字'
        // });
        this.showWarnModal = true;
        this.warnContent =  '处理途径至少2个字';
        return false;
      }
      if (!this.isSubmitZfhf&&!this.report.reportResult.trim()) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '请填写处理结果'
        // });
        this.showWarnModal = true;
        this.warnContent =  '请填写处理结果';
        return false;
      }
      if (!this.isSubmitZfhf&&this.report.reportResult.trim().length < 2) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '处理结果至少2个字'
        // });
        this.showWarnModal = true;
        this.warnContent =  '处理结果至少2个字';
        return false;
      }
      if (!this.isSubmitZfhf&&this.report.reportChannel.trim().length > 100) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '处理途径长度不超过100字'
        // });
        this.showWarnModal = true;
        this.warnContent =  '处理途径长度不超过100字';
        return false;
      }
      if(!this.isSubmitZfhf&&Util.isEmojiCharacter(this.report.reportChannel)){
        // this.$Modal.warning({
        //   title:'温馨提示',
        //   content:"处理途径不能包含emoji表情"
        // });
        this.showWarnModal = true;
        this.warnContent =  "处理途径不能包含emoji表情";
        return false;
      }
      if (!this.isSubmitZfhf&&this.report.reportResult.trim().length > 200) {
        // this.$Modal.warning({
        //   title: '温馨提示',
        //   content: '处理结果长度不超过200字'
        // });
        this.showWarnModal = true;
        this.warnContent =  '处理结果长度不超过200字';
        return false;
      }
      if(!this.isSubmitZfhf&&Util.isEmojiCharacter(this.report.reportResult)){
        // this.$Modal.warning({
        //   title:'温馨提示',
        //   content:"处理结果不能包含emoji表情"
        // });
        this.showWarnModal = true;
        this.warnContent =  "处理结果不能包含emoji表情";
        return false;
      }
      return true;
    },
    bigImg(index){
      this.imgIndex = index
      this.imgSwitch = true
    },
    cancel(){
      this.pageNum = 1;
    },
    reset(){
      this.imgIndex = -1
    },
    /**
     * 切换显示隐藏
     */
    switchShow(){
      var _this=this;
      var params={
        id: this.reportId,
        showFlag: this.handlingReport.showFlag==1 ? 2:1
      };
      this.$Notice.close(_this.noticekey);
      this.$http.post("convphotoManage/switchShowOrHide",params).then(
        function(data){
          if(data.meta.code==0){
            _this.handlingReport.showFlag = params.showFlag;
            _this.$Notice.success({
              title: '温馨提示',
              desc: '操作成功',
              name:_this.noticekey,
              duration:2,
            });
          }else{
            // _this.$Modal.warning({
            //   title: '温馨提示',
            //   content: data.meta.message
            // });
            _this.showWarnModal = true;
            _this.warnContent = data.meta.message;
          }
        }
      ).catch(function(err){
        // _this.$Modal.warning({
        //   title: '温馨提示',
        //   content: err.meta.message
        // });
        _this.showWarnModal = true;
        _this.warnContent = err.meta.message;
      });
    },
    closeModal(){
      this.showWarnModal = false;
    }
  },
  components: {
    'big-img': BigImg
  }
}
